package com.example.workspace.myapp;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText input = (EditText)findViewById(R.id.edt_input);
        final Button call = (Button)findViewById(R.id.btn_call);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String in = input.getText().toString();
                if(PhoneNumberUtils.isGlobalPhoneNumber(in)){
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel://"+in));
                    input.setText("");
                    startActivity(intent);
                }
                else{
                    Toast.makeText(MainActivity.this, "号码格式错误，请重新输入", Toast.LENGTH_SHORT).show();
                    input.setText("");
                }
            }
        });
    }
}
